import axios from 'axios'
import Cookies from 'js-cookie'
// 创建 axios 实例
const service = axios.create({
  // withCredentials: true, // 当跨域请求发送cookie
  timeout: 20000, // 请求超时
  validateStatus(status) {
    return status >= 200 && status < 300
  }
})
Cookies.set('kbzw__user_login', '7Obd08_P1ebax9aX3cPkyPHf6OfT0c-VtKLZ2t_q2eXT29XQo8fWxqSsp8uxo67EppTb3NWplNSYp7Hazd7Q25msl9inlLOO5crY19KUqqGuk6iXq5iupbaxv9Gkwtjz1ePO15CspaOYicfK4t3k4OyMxbaWkqelo7OBx8rir6mkmeStlp-BuOfj5MbHxtbE3t2ooaqZpJStl5vDqcSuwKWV1eLX3IK9xtri4qGBs8nm6OLOqKWvkKaPq66osI-hkaaZyMrfzenLpZaqrqGrlw..')
// post请求头
// service.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;";

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    // console.log(config, "config");

    return config
  },
  (error) => {
    // 请求出错后做的事情
    console.log(error) // debug
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  /**
   * 根据后端自定义code来判断响应状态
   * 下面只是个例子
   * 你也可以通过HTTP状态码来判断
   */
  (response) => {
    // console.log(response, "response");
    return response
  },
  (error) => {
    console.log('err' + error) // for debug

    return Promise.reject(error)
  }
)

// 包装请求
let productionIp = 'http://' + window.location.hostname + '/icc/api'
let request = {}
const base = process.env.NODE_ENV !== 'development' ? productionIp : '/webapi' // url = base url + request url

request.get = (url, params, baseURL = base) => {
  return service.get(url, { params, baseURL })
}

export default request
